
exports.order_submit = function(req,res) {
//问题内容页
    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};
    var ccData = 0;
    resultData.userCouponData = [];
    if(restfulParams.type != 0){
    //任务流创建
    async.waterfall([
        //获取用户的账户余额
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    :   CORE_APIConfig.coreAPI.search_user + "/u_id/" + restfulParams.u_id,
                    method : 'POST',
                    data:{
                        fields:[
                            "u_balance"
                        ].join(",")
                    }

                },
                function (err, data) {

                    resultData.userBalanceData = data.data[0];

                    cb();
                }
            );
        },
        //获取用户拥有的机构代金券信息
        function(cb){
            console.log(123456);
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_user_cash_coupon+"/ucc_is_used/0/ucc_uid/"+restfulParams.u_id+"/o_id/"+restfulParams.o_id,
                    method : 'POST',
                    data:{
                        fields:[
                            "ucc_id",
                            "cc_name",
                            "cc_price",
                            "cc_end_time",
                            "cc_start_time",
                            "cc_type",
                            "cc_desc",
                            "cc_max_price",
                            "cc_fid"
                        ].join(",")
                    }
                },
                function (err, data) {
                    console.log(data.data);
                    if(data.data.length>0){
                        ccData = data.data;
                    }else{
                        resultData.userCouponData = [];
                    }
                    cb();
                }
            );
        },
        /********************************/
        function (cb) {
            if(ccData.length>0){
            if(restfulParams.type == 1){//机构课程代金卷
                for(var i = 0 ; i < ccData.length ; i++){
                    if((ccData[i].cc_max_price <= restfulParams.o_money || ccData[i].cc_max_price == 0.00 ) && ((ccData[i].cc_type == 1 && (ccData[i].cc_fid == 0 || ccData[i].cc_fid == restfulParams.id))|| ccData[i].cc_type==0)){
                        resultData.userCouponData.push(ccData[i]);
                    }
                }
            }else if(restfulParams.type == 2){//机构活动代金卷
                for(var i = 0 ; i < ccData.length ; i++){
                    if((ccData[i].cc_max_price <= restfulParams.o_money || ccData[i].cc_max_price == 0.00)  && ((ccData[i].cc_type == 2 && (ccData[i].cc_fid == 0 || ccData[i].cc_fid == restfulParams.id)) || ccData[i].cc_type==0)){
                        resultData.userCouponData.push(ccData[i]);
                    }
                }
            }
            cb();
            }else{
                cb();
            }
        },


        /**********************************/

        //获取平台代金券信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_user_platform_cash_coupon+"/ucc_is_used/0/ucc_uid/"+restfulParams.u_id,
                    method : 'POST',
                    data:{
                        fields:[
                            "ucc_id",
                            "pc_name",
                            "pc_price",
                            "pc_end_time",
                            "pc_start_time",
                            "pc_des"
                        ].join(",")
                    }
                },
                function (err, data) {
                    if(data.data.length>0){
                    resultData.userPlatformCashCouponData = data.data;
                    }else{
                        resultData.userPlatformCashCouponData = [];
                    }
                    cb();
                }
            );
        },

    ],function(err, result){

        //返回最终拼接结果
        RES.response(res,true,resultData);

    });
    }else{
        //任务流创建
        async.waterfall([
            //获取用户的账户余额
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url    :   CORE_APIConfig.coreAPI.search_user + "/u_id/" + restfulParams.u_id,
                        method : 'POST',
                        data:{
                            fields:[
                                "u_balance"
                            ].join(",")
                        }

                    },
                    function (err, data) {

                        resultData.userBalanceData = data.data[0];

                        cb();
                    }
                );
            },
            //获取约课券信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.search_user_course_coupon+"/ucc_is_used/0/ucc_uid/"+restfulParams.u_id+"/o_id/"+restfulParams.o_id,
                        method : 'POST',
                        data:{
                            fields:[
                                "ucc_id",
                                "cc_name",
                                "cc_end_time",
                                "cc_start_time",
                                "cc_desc"
                            ].join(",")
                        }
                    },
                    function (err, data) {

                        resultData.userCouponData = data.data;

                        cb();
                    }
                );
            },
            //获取平台发放约课券信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.search_user_platform_course_coupon+"/ucc_is_used/0/ucc_uid/"+restfulParams.u_id,
                        method : 'POST',
                        data:{
                            fields:[
                                "ucc_id",
                                "pc_name",
                                "pc_price",
                                "pc_end_time",
                                "pc_start_time",
                                "pc_des"
                            ].join(",")
                        }
                    },
                    function (err, data) {

                        resultData.userPlatformCcourseCouponData = data.data;

                        cb();
                    }
                );
            },

        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        });
    }

}